package com.xy.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by XuYang
 * 2023/4/21
 * Email:544066591@qq.com
 */
public class DateUtil {

    public static String DatePattern = "yyyy-MM-dd";

    public static String TimePattern = "HH:mm:ss";

    public static String DateTimePattern = "yyyy-MM-dd HH:mm:ss";

    public static String getDate(Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate != null) {
            df = new SimpleDateFormat(DatePattern);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }

    public static String getDateTime(String aMask, Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate == null) {

        } else {
            df = new SimpleDateFormat(aMask);
            returnValue = df.format(aDate);
        }

        return (returnValue);
    }

    public static Date getCurrentDate() {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DatePattern);
            String strDate = dateFormat.format(new Date());
            System.out.println(strDate);
            return convertStringToDate(DatePattern, strDate);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getCurrentDateStr() {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DatePattern);
            String strDate = dateFormat.format(new Date());
            System.out.println(strDate);
            return strDate;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date getCurrentDateTime() {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DateTimePattern);
            String strDate = dateFormat.format(new Date());
            System.out.println(strDate);
            return convertStringToDate(DateTimePattern, strDate);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getCurrentDateTimeStr() {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(DateTimePattern);
            String strDate = dateFormat.format(new Date());
            System.out.println(strDate);
            return strDate;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Date convertStringToDate(String aMask, String strDate)
            throws ParseException {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(aMask);
        date = df.parse(strDate);
        return (date);
    }

    /**
     * 获得当前时间字符串（格式："yyyy-MM-dd HH:mm:ss"）
     *
     * @return 时间字符串
     */
    public static String nowTime() {
        return nowDateAddDays(0);
    }

    /**
     * 获取当前日期后aDate天的时间（格式："yyyy-MM-dd HH:mm:ss"）
     *
     * @param aDate
     * @return 时间字符串
     */
    public static String nowDateAddDays(Integer aDate) {
        SimpleDateFormat dateFormatf = new SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, aDate);
        String d = dateFormatf.format(calendar.getTime());
        return d;
    }

    /**
     * 获得当前时间字符串（格式："yyMMddHHmmss"）
     *
     * @return 时间字符串
     */
    public static String nowTimeInyyMMddHHmmss() {
        return nowDateAddDaysForyyMMddHHmmss(0);
    }

    /**
     * 获取当前日期后aDate天的时间（格式："yyMMddHHmmss"）
     *
     * @param aDate
     * @return 时间字符串
     */
    public static String nowDateAddDaysForyyMMddHHmmss(Integer aDate) {
        SimpleDateFormat dateFormatf = new SimpleDateFormat("yyMMddHHmmss");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, aDate);
        String d = dateFormatf.format(calendar.getTime());
        return d;
    }

    public static boolean Time_Between_sTime_And_eTime(String time, String sTime, String eTime) {
        boolean b = false;
        int s1 = Integer.parseInt(sTime.substring(2, 8));
        int e1 = Integer.parseInt(eTime.substring(2, 8));
        int t1 = Integer.parseInt(time.substring(2, 8));
        if ((s1 <= t1) && (t1 <= e1)) {
            if (s1 == e1) {
                int s2 = Integer.parseInt(sTime.substring(8));
                int e2 = Integer.parseInt(eTime.substring(8));
                int t2 = Integer.parseInt(time.substring(8));
                if ((t2 >= s2) && (t2 <= e2)) {
                    b = true;
                }
            } else if (t1 == s1) {
                int s2 = Integer.parseInt(sTime.substring(8));
                int t2 = Integer.parseInt(time.substring(8));
                if (s2 <= t2) {
                    b = true;
                }
            } else if (t1 == e1) {
                int e2 = Integer.parseInt(eTime.substring(8));
                int t2 = Integer.parseInt(time.substring(8));
                if (e2 >= t2) {
                    b = true;
                }
            } else {
                b = true;
            }
        }
        return b;
    }

    public static String convertDateToString(Date aDate) {
        return getDateTime(DatePattern, aDate);
    }

    public static Date convertStringToDate(String strDate)
            throws ParseException {
        Date aDate = null;

        try {
            aDate = convertStringToDate(DatePattern, strDate);
        } catch (ParseException pe) {
            pe.printStackTrace();
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());

        }

        return aDate;
    }

    /**
     * date类型转换为String类型
     * // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
     * // data Date类型的时间
     */
    public static String dateToString(Date data, String formatType) {
        return new SimpleDateFormat(formatType).format(data);
    }

    /**
     * string类型转换为date类型
     * // strTime要转换的string类型的时间，formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日
     * // HH时mm分ss秒，
     * // strTime的时间格式必须要与formatType的时间格式相同
     */
    public static Date stringToDate(String strTime, String formatType) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatType);
        Date date = null;
        try {
            date = formatter.parse(strTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String toString(Date date) {
        if (null != date) {
            return DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.CHINESE).format(date);
        }
        return null;
    }
}
